package com.party.mobile.task;

import com.party.common.utils.StringUtils;
import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.mobile.biz.distributor.DistributorBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 统计分销参数
 * Created by wei.li
 *
 * @date 2017/3/20 0020
 * @time 14:18
 */

@Component(value = "quartzForDistribution")
public class QuartzForDistribution {


    @Autowired
    private IDistributorCountService distributorCountService;

    @Autowired
    private DistributorBizService distributorBizService;

    @Autowired
    private IDistributorRelationService distributorRelationService;

    private static final Logger logger = LoggerFactory.getLogger(QuartzForDistribution.class);

//    @Scheduled( fixedRate = 1000*60*90)
    public void refreshCount(){
        List<Map<String, Object>> mapList = distributorRelationService.getTargetEndTime();

        for (Map<String, Object> stringObjectMap : mapList) {
            try {
                Integer type = (Integer) stringObjectMap.get("type");
                String targetId = (String) stringObjectMap.get("targetId");
                Date endTime = (Date) stringObjectMap.get("endTime");
                if (StringUtils.isEmpty(targetId)) {
                    continue;
                }
                if (!DistributorRelationType.MALL_GOODS.getCode().equals(type) && null == endTime) {
                    continue;
                }
                if (DistributorRelationType.MALL_GOODS.getCode().equals(type)
                        || DistributorRelationType.ARTICLE.getCode().equals(type)) {
                    DistributorRelation t = new DistributorRelation();
                    t.setTargetEndDate(null);
                    t.setTargetId(targetId);
                    distributorRelationService.updateByTargetId(t);
                } else if (endTime != null) {
                    DistributorRelation t = new DistributorRelation();
                    t.setTargetEndDate(endTime);
                    t.setTargetId(targetId);
                    distributorRelationService.updateByTargetId(t);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        List<DistributorCount> list = distributorCountService.list(new DistributorCount());
        for (DistributorCount distributorCount : list){
            try {
                DistributorRelation distributorRelation = distributorRelationService.get(distributorCount.getDistributorRalationId());
                if (null == distributorRelation || null == distributorRelation.getType()) {
                    continue;
                }
                distributorBizService.refreshCount(distributorCount, distributorRelation.getType());
            } catch (Exception e) {
                logger.error("更新分销统计异常", e);
                continue;
            }
        }
    }
}
